#include "customprinter.h"
#include "ui_customprinter.h"
#include "userlogger.h"
#include "msgbox.h"
#include "project.h"
#include "shadow.h"
#include <QListView>
#include <QPrinterInfo>
#include <QTextDocument>

QString CustomPrinter::m_defaultPrinter;

CustomPrinter::CustomPrinter(const QString &document, const PageSize &pageSize, const Orientation &orientation)
    : ui(new Ui::CustomPrinter)
    , m_window(Q_NULLPTR)
    , m_document(document)
{
    ui->setupUi(this);
    initWidget(pageSize, orientation);
}

CustomPrinter::CustomPrinter(const QWidget *window, const PageSize &pageSize, const Orientation &orientation)
    : ui(new Ui::CustomPrinter)
    , m_window(const_cast<QWidget *>(window))
{
    ui->setupUi(this);
    initWidget(pageSize, orientation);
}

CustomPrinter::~CustomPrinter()
{
    delete ui;
}

int CustomPrinter::show(const QString &message)
{
    m_message = message;
    return QDialog::exec();
}

void CustomPrinter::exportToPdf(const QPixmap picture, const QString &file, const CustomPrinter::PageSize &pageSize, const CustomPrinter::Orientation &orientation)
{
    QPrinter printer(QPrinter::HighResolution);
    printer.setFullPage(true);
    printer.setPageMargins(0, 0, 0, 0,QPrinter::Millimeter);
    printer.setPageSize(QPageSize((pageSize == A4) ? QPageSize::A4 : QPageSize::A5));
    printer.setOrientation((orientation == Horizontal) ? QPrinter::Landscape : QPrinter::Portrait);
    printer.setOutputFileName(file);

    QPainter painter;
    painter.begin(&printer);
    QRect rect = painter.viewport(); //获取painter的视口区域
    double paintWidth = rect.width();
    double pixmapWidth = picture.width() + 50;
    double scaleFactor = paintWidth / pixmapWidth; //计算painter视口区域与抓取图片区域的尺寸比例因子
    painter.scale(scaleFactor, scaleFactor); //绘制时按照比例因子放大
    painter.drawPixmap(20, 20, picture);
    painter.end();
}

void CustomPrinter::initWidget(const PageSize &pageSize, const Orientation &orientation)
{
    if (windowWidth >= 1280)
    {
        this->setGeometry(0, 0, 1280, 800);
        this->setProperty("cssSize", "large");
    }
    else
    {
        this->setGeometry(0, 0, 1024, 600);
    }

    setParent(Project::Instance());
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
    shadow->setColor(QColor(0,0,0,150));
    shadow->setOffset(1, 1);
    shadow->setBlurRadius(16);
    ui->widget->setGraphicsEffect(shadow);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(windowFlags() | Qt::SubWindow);

    refreshPrinters();

    if (pageSize == A4)
    {
        ui->radioA4->setChecked(true);
    }
    else
    {
        ui->radioA5->setChecked(true);
    }

    if (orientation == Horizontal)
    {
        ui->radioHorizontal->setChecked(true);
    }
    else
    {
        ui->radioVertical->setChecked(true);
    }
}

void CustomPrinter::refreshPrinters()
{
    QString currentPrinter = m_defaultPrinter;
    ui->cbxPrinter->clear();
    ui->cbxPrinter->addItems(QPrinterInfo::availablePrinterNames());
    ui->cbxPrinter->setCurrentText(currentPrinter);
    ui->cbxPrinter->setView(new QListView());
}

void CustomPrinter::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(255, 255, 255, 0));
}

void CustomPrinter::on_btnConfirmPrint_clicked()
{
#ifndef QT_NO_PRINTER
    QString printerName = ui->cbxPrinter->currentText();
    if (printerName.isEmpty())
    {
        MsgBox::information(this, tr("Print"), tr("no printer selected"));
        return;
    }

    QPrinter printer(QPrinterInfo::printerInfo(printerName), QPrinter::ScreenResolution);
    printer.setFullPage(true);
    printer.setPageMargins(0,0,0,0,QPrinter::Millimeter);
    printer.setPageSize(QPageSize(ui->radioA4->isChecked() ? QPageSize::A4 : QPageSize::A5));
    printer.setOrientation(ui->radioVertical->isChecked() ? QPrinter::Portrait : QPrinter::Landscape);

    if (m_window)
    {
        QPixmap pixmap = m_window->grab();
        QPainter painter;
        painter.begin(&printer);
        QRect rect = painter.viewport(); //获取painter的视口区域
        double paintWidth = rect.width();
        double pixmapWidth = pixmap.width() + 50;
        double scaleFactor = paintWidth / pixmapWidth; //计算painter视口区域与抓取图片区域的尺寸比例因子
        painter.scale(scaleFactor, scaleFactor); //绘制时按照比例因子放大
        painter.drawPixmap(20, 20, pixmap);
        painter.end();
    }
    else
    {
        QTextDocument textDocument;
        textDocument.setPageSize(QSizeF(printer.pageRect().size()));
        textDocument.setDocumentMargin(20);
        textDocument.setHtml(m_document);
        textDocument.print(&printer);
    }

    UserLogger::log(m_message);
    this->hide();
//    MsgBox::information(this,QString(tr("Print")),QString(tr("Report printed ok!")));
#endif
}

void CustomPrinter::on_btnRefresh_clicked()
{
    refreshPrinters();
}

void CustomPrinter::on_btnCancelPrint_clicked()
{
    this->hide();
}

void CustomPrinter::on_btnClose_clicked()
{
    this->hide();
}

void CustomPrinter::on_cbxPrinter_currentTextChanged(const QString &arg1)
{
    if (m_defaultPrinter != arg1)
    {
        m_defaultPrinter = arg1;
    }
}
